<% title "Service Information Tables" %> 
<p>
  Download:
  <%= link_to "CSV", service_information_tables_path(format: "csv") %> |
  <%= link_to "Excel", service_information_tables_path(format: "xls") %>
</p>

<% idFileRedBee = SelectFile.where(:server => "RedBee").first.id%>
<% idSIT = ServiceInformationTable.where(:select_file_id => idFileRedBee).first.id %>
<% idPLT = ProgramLocationTable.where(:select_file_id => idFileRedBee).first.id%>
<% idPIT = ProgramInformationTable.where(:select_file_id => idFileRedBee).first.id%>
<% listOfServices = ServiceInformation.where(:service_information_table_id=>idSIT).select(:id).map{|i| i.id }%>
<% listOfServicesAxelS = ServiceInformation.select{|i| i.service_information_table_id != idSIT}.map{|i| [i.name, i.id]}%>
<% #programInformations = ProgramInformation.select{|i| i.program_information_table_id==idPIT}.count
%>
<% #programInformationsAxelS = ProgramInformation.select{|i| i.program_information_table_id!=idPIT}.count
%>

<table class='tabla'>
	<tr  class='modo1'>
    	<th colspan="2">Default channels (<%= DefaultChannel.count%> services)</th>
    	<th colspan="6">RedBee (<%= ServiceInformation.where(:service_information_table_id=>idSIT).count %> services)</th>
    	<th colspan="7">AxelS (<%= ServiceInformation.select{|i| i.service_information_table_id != 1}.map{|i| i.name}.uniq.count%> services)</th>
  </tr>
  <tr  class='modo1'>
    <th>Name</th>
    <th>Expected Language</th>
    <th>Name</th>
    <th>Language</th>
    <th>Audio attr</th>
    <th>Video attr</th>
    <th>Start</th>
    <th>End</th>
    <th>Name</th>
    <th>Language</th>
    <th>Audio attr</th>
    <th>Video attr</th>
    <th>Start</th>
    <th>End</th>
    <th>Files</th>
  </tr>
  
  <% for default_channel in DefaultChannel.all %>
    <tr  class='modo2'>
      <td><%= default_channel.name %></td>
      <td><%= default_channel.language %></td>
      <% si = ServiceInformation.where(:service_information_table_id=>idSIT).where(:name=>default_channel.name).first%>
      <% siAxel = ServiceInformation.select{|i| i.service_information_table_id != idSIT && i.name==default_channel.name}%>
      <% if (si==nil)%>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      <% else%>
      	<td><%= si.name %></td>
      	<%  listOfServices.delete(si.id) %>
      	<td>
      		<%
			serviceId = ServiceInformation.select{|i| i.name==si.name && i.service_information_table_id==idPIT}.first.serviceId
			logger.debug " -- - - - serviceId:#{serviceId}"
			if(!Schedule.where(:service_id=>serviceId).empty?)
 				scheduleId = Schedule.where(:service_id=>serviceId).first.id
 				scheduleEventsCRID = ScheduleEvent.where(:schedule_id=>scheduleId).map{|i| i.programCrid}
 				
 				programInformations = ProgramInformation.select{|i| i.program_information_table_id==1 && scheduleEventsCRID.include?(i.crid)}
 				if(!programInformations.empty?)
 				
 				solo1campo = programInformations.map{|i| i.language}
 				totalElementos = solo1campo.count
 				x = Hash[solo1campo.uniq.map{ |i| [i, solo1campo.count(i)] }]
      		%>
      		<%= 100*x.first[1]/totalElementos %>% in <%=x.first[0]%>
      		<% end%>
      		<% end%>
      		 </td>
      	<% 
      	#scheduleEventsAV = ScheduleEvent.where(:schedule_id=>scheduleId).map{|i| i.avattribute}
      		logger.debug" - - - --  -scheduleId:#{scheduleId}"
	      	scheduleEventsAV = ScheduleEvent.where(:schedule_id=>scheduleId).map{|i| i.id}
	      	scheduleEventsTotal = ScheduleEvent.where(:schedule_id=>scheduleId).count
	      	logger.debug" - - - -scheduleEventsTotal:#{scheduleEventsTotal}"
		if(scheduleEventsTotal!=0)
	      	avattributes = Avattribute.select{|i| scheduleEventsAV.include?(i.schedule_event_id)}
	      	itemVideo = avattributes.select{|i| i.avtype=='video'}.count
	      	itemAudio = avattributes.select{|i| i.avtype=='audio'}.count
	      	logger.debug "itemVideo:#{itemVideo}"
	      	logger.debug "itemAudio:#{itemAudio}"
	      	%>
	      	<td><%= 100*itemAudio/scheduleEventsTotal %>%</td>
	      	<td><%= 100*itemVideo/scheduleEventsTotal %>%</td>
	     <%
	  	else%>
	    	<td> - </td>
	      	<td> - </td>
	    <%end
	     %>
      	<td><%= Schedule.where(:program_location_table_id=>idPLT).where(:service_id=>si.serviceId).first.start.first(10) %></td>
      	<td><%= Schedule.where(:program_location_table_id=>idPLT).where(:service_id=>si.serviceId).first.end.first(10)  %></td>
      <% end%>
      <% if (siAxel.empty?)%>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>
      	<td> - </td>	
      <% else%>
      	<td><%= siAxel.first.name %></td>
      	<%  for item in listOfServicesAxelS.select{|i| i[0]==siAxel.first.name} %>
      	<%  	listOfServicesAxelS.delete(item) %>
      	<%  end %>
      	<td>
		<%
			serviceIdAxel = ServiceInformation.select{|i| i.name==siAxel.first.name && i.service_information_table_id!=idPIT}.first.serviceId
			if(!Schedule.where(:service_id=>serviceIdAxel).empty?)
 				scheduleIdAxel = Schedule.where(:service_id=>serviceIdAxel).map{|i| i.id}
 				scheduleEventsCRIDAxel = ScheduleEvent.select{|i| scheduleIdAxel.include?(i.schedule_id)}.map{|i| i.programCrid}
 				
 				programInformationsAxel = ProgramInformation.select{|i| i.program_information_table_id!=1 && scheduleEventsCRIDAxel.include?(i.crid)}
 				if(!programInformationsAxel.empty?)
 				
 				solo1campoAxel = programInformationsAxel.map{|i| i.language}
 				totalElementosAxel = solo1campoAxel.count
 				xAxel = Hash[solo1campoAxel.uniq.map{ |i| [i, solo1campoAxel.count(i)] }]
      		%>
      		<%= 100*xAxel.first[1]/totalElementosAxel %>% in <%=xAxel.first[0]%>
      	<% 
      	#scheduleEventsAV = ScheduleEvent.where(:schedule_id=>scheduleId).map{|i| i.avattribute}
      		logger.debug" - - - --  -scheduleIdAxel#{scheduleIdAxel}"
	      	scheduleEventsAV = ScheduleEvent.where(:schedule_id=>scheduleIdAxel).map{|i| i.id}
	      	scheduleEventsTotal = ScheduleEvent.where(:schedule_id=>scheduleIdAxel).count
	      	logger.debug" - - - -scheduleEventsTotal:#{scheduleEventsTotal}"
		if(scheduleEventsTotal!=0)
	      	avattributes = Avattribute.select{|i| scheduleEventsAV.include?(i.schedule_event_id)}
	      	itemVideo = avattributes.select{|i| i.avtype=='video'}.count
	      	itemAudio = avattributes.select{|i| i.avtype=='audio'}.count
	      	logger.debug "itemVideo:#{itemVideo}"
	      	logger.debug "itemAudio:#{itemAudio}"
	      	%>
	      	<td><%= 100*itemAudio/scheduleEventsTotal %>%</td>
	      	<td><%= 100*itemVideo/scheduleEventsTotal %>%</td>
	     <%
	  	else%>
	    	<td> - </td>
	      	<td> - </td>
	    <%end
	     %>
		</td>
      	<td><%= Schedule.select{|i| i.service_id==siAxel.first.serviceId.to_s && i.program_location_table_id!=1}.first.start.first(10) %></td>
      	<td><%= Schedule.select{|i| i.service_id==siAxel.first.serviceId.to_s && i.program_location_table_id!=1}.last.end.first(10)  %></td>
      	<td><%= ServiceInformation.where(:name=>default_channel.name).select{|i| i.service_information_table_id != idSIT}.count%></td>
      <% end%>
    </tr>
  <% end %>
  <% for serviceId in listOfServices %>
  	 <tr  class='modo2'>
  		<td> - </td>
      	<td> - </td>
      	<td><%= ServiceInformation.find(serviceId).name %></td>
      	<td>
		<%
		serviceId = ServiceInformation.select{|i| i.name==ServiceInformation.find(serviceId).name && i.service_information_table_id==idPIT}.first.serviceId
		logger.debug " -- - - - serviceId:#{serviceId}"
		if(!Schedule.where(:service_id=>serviceId).empty?)
			scheduleId = Schedule.where(:service_id=>serviceId).first.id
			scheduleEventsCRID = ScheduleEvent.where(:schedule_id=>scheduleId).map{|i| i.programCrid}
			
			programInformations = ProgramInformation.select{|i| i.program_information_table_id==1 && scheduleEventsCRID.include?(i.crid)}
			if(!programInformations.empty?)
			
			solo1campo = programInformations.map{|i| i.language}
			totalElementos = solo1campo.count
			x = Hash[solo1campo.uniq.map{ |i| [i, solo1campo.count(i)] }]
	  		%>
	  		<%= 100*x.first[1]/totalElementos %>% in <%=x.first[0]%>
	  		<% end%>
  		<% end%>
		</td>
      	<td> - </td>
	    <td> - </td>
      	<td><%= Schedule.where(:service_id=>serviceId.to_s).first.start.first(10) %></td>
      	<td><%= Schedule.where(:service_id=>serviceId.to_s).first.end.first(10)  %></td>
      	<% if (listOfServicesAxelS.select{|i| i[0]==(ServiceInformation.where(:serviceId=>serviceId).first.name)}.empty?)%>
	      	<td> - </td>
	      	<td> - </td>
	      	<td> - </td>
      		<td> - </td>
	      	<td> - </td>
		    <td> - </td>
		    <td> - </td>
      	<% else%>
      		<% siAxel = ServiceInformation.select{|i| i.service_information_table_id != idSIT && i.name==ServiceInformation.find(serviceId).name}%>
      		<td><%= siAxel.first.name %></td>
	      	<%  for item in listOfServicesAxelS.select{|i| i[0]==siAxel.first.name} %>
	      		<%  	listOfServicesAxelS.delete(item) %>
	      	<%  end %>
	      	<td>
			<%
			serviceIdAxel = ServiceInformation.select{|i| i.name==siAxel.first.name && i.service_information_table_id!=idPIT}.first.serviceId
			if(!Schedule.where(:service_id=>serviceIdAxel).empty?)
 				scheduleIdAxel = Schedule.where(:service_id=>serviceIdAxel).map{|i| i.id}
 				scheduleEventsCRIDAxel = ScheduleEvent.select{|i| scheduleIdAxel.include?(i.schedule_id)}.map{|i| i.programCrid}
 				
 				programInformationsAxel = ProgramInformation.select{|i| i.program_information_table_id!=1 && scheduleEventsCRIDAxel.include?(i.crid)}
 				if(!programInformationsAxel.empty?)
 				
 				solo1campoAxel = programInformationsAxel.map{|i| i.language}
 				totalElementosAxel = solo1campoAxel.count
 				xAxel = Hash[solo1campoAxel.uniq.map{ |i| [i, solo1campoAxel.count(i)] }]
 				logger.debug " - - --  -- - - - xAxel:#{xAxel}"
      		%>
      		<%= 100*xAxel.first[1]/totalElementosAxel %>% in <%=xAxel.first[0]%>
      		<% end%>
      		<% end%>
      		</td>
      		<% 
	      	#scheduleEventsAV = ScheduleEvent.where(:schedule_id=>scheduleId).map{|i| i.avattribute}
	      		logger.debug" - - - --  -scheduleIdAxel#{scheduleIdAxel}"
		      	scheduleEventsAV = ScheduleEvent.where(:schedule_id=>scheduleIdAxel).map{|i| i.id}
		      	scheduleEventsTotal = ScheduleEvent.where(:schedule_id=>scheduleIdAxel).count
		      	logger.debug" - - - -scheduleEventsTotal:#{scheduleEventsTotal}"
			if(scheduleEventsTotal!=0)
		      	avattributes = Avattribute.select{|i| scheduleEventsAV.include?(i.schedule_event_id)}
		      	itemVideo = avattributes.select{|i| i.avtype=='video'}.count
		      	itemAudio = avattributes.select{|i| i.avtype=='audio'}.count
		      	logger.debug "itemVideo:#{itemVideo}"
		      	logger.debug "itemAudio:#{itemAudio}"
		      	%>
		      	<td><%= 100*itemAudio/scheduleEventsTotal %>%</td>
		      	<td><%= 100*itemVideo/scheduleEventsTotal %>%</td>
		     <%
		  	else%>
		    	<td> - </td>
		      	<td> - </td>
		    <%end
		     %>
	      	<td><%= Schedule.select{|i| i.service_id==siAxel.first.serviceId.to_s && i.program_location_table_id!=1}.first.start.first(10) %></td>
      		<td><%= Schedule.select{|i| i.service_id==siAxel.first.serviceId.to_s && i.program_location_table_id!=1}.last.end.first(10)  %></td>
      		<td><%= ServiceInformation.where(:name=>ServiceInformation.find(serviceId).name).select{|i| i.service_information_table_id != idSIT}.count%></td>
      	<% end%>
      </tr>
  <% end %>
  <% for serviceAxel in listOfServicesAxelS.uniq!{|s| s.first} %>
  <tr  class='modo2'>
  	<td> - </td>
    <td> - </td>
    <td> - </td>
    <td> - </td>
    <td> - </td>
    <td> - </td>
    <td> - </td>
    <td> - </td>
    <td> - </td>
    <td> - </td>
    <td><%= serviceAxel[0] %></td>
    <td> - </td>
    <% siAxel = ServiceInformation.select{|i| i.service_information_table_id != idSIT && i.name==serviceAxel[0]}%>
	<td><%= Schedule.select{|i| i.service_id==siAxel.first.serviceId.to_s && i.program_location_table_id!=1}.first.start.first(10) %></td>
    <td><%= Schedule.select{|i| i.service_id==siAxel.first.serviceId.to_s && i.program_location_table_id!=1}.last.end.first(10)  %></td>
    <td><%= ServiceInformation.where(:name=>serviceAxel[0]).select{|i| i.service_information_table_id != idSIT}.count%></td>
  </tr>
  <% end%>
</table>
